package Util::QQ::QQAuthenticate;
use Moose;
use namespace::autoclean;
use JSON;
use WWW::Curl::Easy;
use HTTP::Response;
use Digest::MD5 qw(md5_hex);
use URI::Escape;
 use IO::File;

sub get_userinfo {
	my ($self, $code, $qq_auth_access, $appId, $appKey, $qq_redirect_uri) = @_;
	my %sysParams;
	$sysParams{"method"} = "Users.getInfo";
	$sysParams{"v"} = "1.0";
        my $time = `date +"%Y-%m-%d %H:%M:%S"`;
        chomp($time);
        $sysParams{"call_id"} = $time;
	my %resData;
	my $post_fields = "grant_type=authorization_code&client_id=".$appId."&client_secret=".$appKey."&code=".$code."&redirect_uri=".$qq_redirect_uri;
	$resData{"post_fields"} = $post_fields;
	my $curl = WWW::Curl::Easy->new;
	$curl->setopt(CURLOPT_HEADER,1);
	$curl->setopt(CURLOPT_POSTFIELDS, $post_fields);
	$curl->setopt(CURLOPT_URL, $qq_auth_access);

	my $response;
	$curl->setopt(CURLOPT_WRITEDATA,\$response);

	my $retcode = $curl->perform;
	$resData{"retcode"} = $retcode;
	$resData{"error"} = $curl->strerror($retcode);
	$resData{"errorbuf"} = $curl->errbuf;
	if ($retcode == 0) {
		$response = HTTP::Response->parse($response);
		my $xml = $response->decoded_content;
		my @arrays = split(/access_token=/,$xml); 
		my @tokens = split(/&/,$arrays[1]);
		my $token = $tokens[0];
		$resData{"headurl"} = "zhou";
		$sysParams{"access_token"} = $token;
		$resData{'code'} = 0;
		
		$resData{"access_token"} = $sysParams{"access_token"};
	} else {
			$resData{'code'} = 1;
	}
	return \%resData;
}

sub get_openId {
	my ($self, $access_token, $qq_auth_openid) = @_;
	my %sysParams;
	$sysParams{"method"} = "Users.getInfo";
	$sysParams{"v"} = "1.0";
        my $time = `date +"%Y-%m-%d %H:%M:%S"`;
        chomp($time);
        $sysParams{"call_id"} = $time;
	my %resData;
	my $post_fields = "access_token=".$access_token;
	$resData{"post_fields"} = $post_fields;
	my $curl = WWW::Curl::Easy->new;
	$curl->setopt(CURLOPT_HEADER,1);
	$curl->setopt(CURLOPT_POSTFIELDS, $post_fields);
	$curl->setopt(CURLOPT_URL, $qq_auth_openid);

	my $response;
	$curl->setopt(CURLOPT_WRITEDATA,\$response);

	my $retcode = $curl->perform;
	$resData{"retcode"} = $retcode;
	$resData{"error"} = $curl->strerror($retcode);
	$resData{"errorbuf"} = $curl->errbuf;
	if ($retcode == 0) {
		$response = HTTP::Response->parse($response);
		my $xml = $response->decoded_content;
		#my $json = from_json($xml);
		my @arrays = split(/"openid":"/,$xml); 
		my @tokens = split(/"/,$arrays[1]);
		my $openId= $tokens[0];
		$resData{"headurl"} = "zhou";
		$sysParams{"openId"} = $openId;
		$resData{'code'} = 0;
		
		$resData{"openId"} = $sysParams{"openId"};
	} else {
			$resData{'code'} = 1;
	}
	return \%resData;
}


sub get_nick_name {
	my ($self, $access_token, $openId, $appId, $qq_auth_userinfo) = @_;
	my %sysParams;
	$sysParams{"method"} = "Users.getInfo";
	$sysParams{"v"} = "1.0";
        my $time = `date +"%Y-%m-%d %H:%M:%S"`;
        chomp($time);
        $sysParams{"call_id"} = $time;
	my %resData;
	my $post_fields = "access_token=".$access_token."&oauth_consumer_key=".$appId."&openid=".$openId;
	$resData{"post_fields"} = $post_fields;
	my $curl = WWW::Curl::Easy->new;
	$curl->setopt(CURLOPT_HEADER,1);
	$curl->setopt(CURLOPT_POSTFIELDS, $post_fields);
	$curl->setopt(CURLOPT_URL, $qq_auth_userinfo);

	my $response;
	$curl->setopt(CURLOPT_WRITEDATA,\$response);

	my $retcode = $curl->perform;
	$resData{"retcode"} = $retcode;
	$resData{"error"} = $curl->strerror($retcode);
	$resData{"errorbuf"} = $curl->errbuf;
	if ($retcode == 0) {
		$response = HTTP::Response->parse($response);
		my $xml = $response->decoded_content;
		my @arrays = split(/"nickname": "/,$xml); 
		my @tokens = split(/"/,$arrays[1]);
		my $nickName= $tokens[0];
		$resData{"headurl"} = "zhou";
		$sysParams{"nickName"} = $nickName;
		$resData{'code'} = 0;
		
		$resData{"nickName"} = $sysParams{"nickName"};
	} else {
			$resData{'code'} = 1;
	}
	return \%resData;
}
__PACKAGE__->meta->make_immutable;

1;
